import { useUserStore } from "./../../../stores/user";
import type { Book } from "@/domain/book";
import { ref, onMounted, watch } from "vue";
import * as booksApi from "@/api/books";

const getBooks = function (books: any, flag: number = 0, params: any = {}) {
  books.value = [];
  if (flag === 0) {
    booksApi.getBooks().then((res) => {
      books.value = res;
    });
  } else if (flag === 1) {
    booksApi.getBooksByIsbn(params).then((res) => {
      books.value = res;
    });
  } else if (flag === 2) {
    booksApi.getBooksByName(params).then((res) => {
      books.value = res;
    });
  } else if (flag === 3) {
    booksApi.getBooksByAuthor(params).then((res) => {
      books.value = res;
    });
  }
  console.log("🚀 ~ file: useBooks.ts ~ line 25 ~ getBooks ~ books", books)
};

export {
  getBooks,
}

export default function useBooks(flag: number = 0, params: any = {}) {
  const books = ref<Book[]>([]);

  onMounted(() => {
    // getBooks(books, flag, params);
  });
  const store = useUserStore();
  watch(
    () => store.isLogin,
    () => {
      // getBooks(books, flag, params);
    }
  );
  return books;
}
